//
//  RectPainterView.mm
//  ChartRefactor
//
//  Created by CaoJun on 15/3/13.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterRect.h"

using namespace std;
using namespace ytx;

@implementation PainterRect

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    Quotation * data = indexPosition->getData();

    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();


    /*drawBack*/
    CGFloat width = rect.getCanvasWidth(), height = rect.getCanvasHeight();
    [ctx fillRectWithX:0 withY:0 withWidth:width withHeight:height withFillColor:[NSString stringWithUTF8String:style.backColor.c_str()]];

    /*fillRect*/
    [ctx fillRectWithX:rect.getBeginX() withY:rect.getBeginY() withWidth:rect.getRectWidth() withHeight:rect.getRectHeight() withFillColor:[NSString stringWithUTF8String:style.rectColor.c_str()]];

    /*strokeRect*/
    [ctx setLineWidth:style.rectLineWidth];
    [ctx storkeRectWithX:rect.getBeginX() withY:rect.getBeginY() withWidth:rect.getRectWidth() withHeight:rect.getRectHeight() withStrokeColor:[NSString stringWithUTF8String:style.rectLineColor.c_str()]];

    /*drawRow*/
    float distanceX = 0;
    int row = rect.getRow();
    float length = style.dashLineLength;
    float distance = style.dashLineDistance;
    bool isDashLine = Config::getInstance()->chartTemplate.isDashLine;

    /* 画方格
     * 在PainterRect drawRow使用
     * avg 和 k线应当重写
     */
    vector<int> drawRowData = [self getDrawRowData:row widthType:rect.getWidthType() withData:data];

    distanceX = rect.getRectWidth() / row;

    float tempX = rect.getBeginX() + distanceX;

    [ctx setLineWidth:style.coordinateWidth ];
    [ctx setStrokeStyle:[NSString stringWithUTF8String:style.coordinateColor.c_str()]];
    for (vector<int>::const_iterator i = drawRowData.begin() + 1; i != drawRowData.end(); ++i, tempX += distanceX) {
        int isDraw = *i;
        if (isDraw) {
            CGFloat lengths[2] = {length, distance};
            
            if (isDashLine) {
                [ctx drawDashedWithLength: lengths withSize:2 withX1:tempX withY1:rect.getBeginY() withX2:tempX withY2:rect.getEndY()];
            }
            else{
                [ctx drawLineWithX1:tempX withY1:rect.getBeginY() withX2:tempX withY2:rect.getEndY()];
            }
        }
    }

    [self drawColumn:cgrect withContext:ctx withViewModelIndexPosition:indexPosition];


}

- (void)drawColumn:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    Rectangle & rect = indexPosition->getRectangle();
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;

    float length = style.dashLineLength;
    float distance = style.dashLineDistance;
    bool isDashLine = config->chartTemplate.isDashLine;

    float tempY = rect.getBeginY();
    int i = 0;
    for (i = 0; i < rect.getColumn() - 1; i++) {
        tempY += rect.getDistanceY();
        if (i == rect.getColumn() / 2 - 1 || !isDashLine){
            [ctx drawLineWithX1:rect.getBeginX() withY1:tempY withX2:rect.getEndX() withY2:tempY];// 实线
        }
        else{
            CGFloat lengths[2] = {length, distance};
            [ctx drawDashedWithLength: lengths withSize:2 withX1:rect.getBeginX() withY1:tempY withX2:rect.getEndX() withY2:tempY];// 虚线
        }
    }

}

- (vector<int>) getDrawRowData:(int) rowCount widthType:(SizeType::SizeType) widthType withData:(Quotation *) data
{
    const vector<string> & timeString = data->getBottomSideText(widthType);
    vector<int> drawRowData;
    for (vector<string>::const_iterator it = timeString.begin(); it != timeString.end(); ++it) {
        if((*it) == ""){
            drawRowData.push_back(0);
        }
        else{
            drawRowData.push_back(1);
        }
    }
    return drawRowData;
}
//{
//    vector<int> drawRowData(rowCount, true);
//    return drawRowData;
//}


@end
